package ctgu;

/**
 * @author 2019111120 佘拓
 */
public class NextDate {

    @SuppressWarnings("checkstyle:JavadocVariable")
    static final String ERROR = "日期输入错误";

    /**
     * @param line   输入的日期
     * @param length 长度
     * @return 返回下一天的日期的字符串或者错误信息
     */
    public String nextDate(final String line, final int length) {
        String result;
        //处理字符串
        final String[] str = line.replaceAll("[\\u4e00-\\u9fa5]", "-").split("-");
        if (str.length == length) {
            final int year = Integer.parseInt(str[0]);
            final int month = Integer.parseInt(str[1]);
            final int day = Integer.parseInt(str[2]);
            if (checkYear(year) && checkMonth(month) && checkDay(day, month)) {
                if (bigMonth(month)) {
                    result = getDate(year, month, day, 31);
                } else {
                    result = getDate(year, month, day, 30);
                }
            } else {
                result = ERROR;
            }
        } else {
            result = ERROR;
        }
        return result;
    }

    /**
     * @param year      年份
     * @param day       日
     * @param month     月
     * @param dayNumber 该月的天数
     * @return 返回下一天的日期的字符串
     */
    private String getDate(final int year, final int month, final int day, final int dayNumber) {
        int nMonth = month;
        int nYear = year;
        int nDay;
        String result;
        nMonth += day / dayNumber;
        nDay = day + 1 - dayNumber * (day / dayNumber);
        nYear += nMonth / 12;
        nMonth = nMonth - 12 * (nMonth / 12);
        if (nMonth - month < 1 || nDay == 1) {
            result = nYear + "年" + nMonth + "月" + nDay + "日";
        } else {
            result = ERROR;
        }
        return result;
    }

    /**
     * @param year 年份
     * @return 返回该年份是否在范围内
     */
    public boolean checkYear(final int year) {
        return year >= 1964 && year <= 2050;
    }

    /**
     * @param month 月份
     * @return 月份是否在范围的的布尔值
     */
    public boolean checkMonth(final int month) {
        return month <= 12 && month >= 1;
    }

    /**
     * @param month 月份
     * @return 返回该月份是否为大月的布尔值
     */
    public boolean bigMonth(final int month) {
        boolean result;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12: {
                result = true;
                break;
            }
            default: {
                result = false;
                break;
            }
        }
        return result;
    }

    /**
     * @param day   日
     * @param month 月份
     * @return 返回日数是否存在
     */
    public boolean checkDay(final int day, final int month) {
        boolean result;
        if (bigMonth(month)) {
            result = day <= 31 && day >= 1;
        } else {
            result = day <= 30 && day >= 1;
        }
        return result;
    }
}


